<?php
    require("./Utils.php");

    $conn = getConnection();

    $sql = "SELECT id FROM pet_type";
    $ids = query_list($conn, $sql);

    $data = array();
    foreach($ids as $type_id){
        $sql = "SELECT t.type type, t.image image,p.id, p.name name, p.image pet_image
                FROM pet_type t LEFT JOIN pet p 
                ON p.type_id = t.id
                WHERE t.id = ? LIMIT 4";
    
        $res = query_list($conn, $sql, $type_id['id']);

        $type_pets = array();

        $type_pets['type'];
        $type_pets['image'];
        $type_pets['pet_list'] = array();

        foreach($res as $obj){
            $type_pets['type'] = $obj['type'];
            $type_pets['image'] = $obj['image'];

            $pet = array();
            $pet['name'] = $obj['name'];
            $pet['image'] = $obj['pet_image'];
            $pet['id'] = $obj['id'];
            $pet['type'] = $type_pets['type'];

            array_push($type_pets['pet_list'], $pet);
        }

        array_push($data, $type_pets);
    }

    echo getJson(200, '获取成功', $data);
?>